﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using EasyToHelp.Business;
using EasyToHelp.Common;
using EasyToHelp.Entity;
using EasyToHelp.Service;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace EasyToHelp.WAP.Controllers
{
    public class UserController : Controller
    {
        private readonly IMemberService Member;
        public UserController(IMemberService _Member)
        {
            Member = _Member;
        }
        #region 页面
        public IActionResult Login()
        {
            return View();
        }
        public IActionResult Reg()
        {
            return View();
        }
        #endregion

        #region 方法

        #region 注册
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult RegAction(Member model)
        {
            if (string.IsNullOrWhiteSpace(model.UserName))
            {
                return Json(ResultInfo.Error("请输入用户名！"));
            }
            var u = Member.GetList().Where(x => x.UserName == model.UserName).FirstOrDefault();
            if (u != null)
            {
                return Json(ResultInfo.Error("用户名已存在！"));
            }
            if (!RegexHelper.IsNormalChar(model.UserName))
            {
                return Json(ResultInfo.Error("请设置字符，字母、数字、下划线的组合的用户名！"));
            }
            if (!RegexHelper.IsLengthStr(model.UserPwd, 6, 20))
            {
                return Json(ResultInfo.Error("请设置6-20位的密码！"));
            }
            if (string.IsNullOrWhiteSpace(model.UserPwd))
            {
                return Json(ResultInfo.Error("请输入用户密码！"));
            }
            if (string.IsNullOrWhiteSpace(model.Emaile))
            {
                return Json(ResultInfo.Error("请输入用户邮箱！"));
            }
            else if (!RegexHelper.IsEmail(model.Emaile))
            {
                return Json(ResultInfo.Error("邮箱格式不正确！"));
            }
            if (string.IsNullOrWhiteSpace(model.Mobile))
            {
                return Json(ResultInfo.Error("请输入用户手机号！"));
            }
            else if (!RegexHelper.IsMobileNumberSimple(model.Mobile))
            {
                return Json(ResultInfo.Error("手机号格式不正确！"));
            }
            model.AddTime = DateTime.Now;
            if (Member.Add(model) > 0)
            {
                return Json(ResultInfo.Success("注册成功！"));
            }
            return Json(ResultInfo.Error("注册失败！"));
        }
        #endregion

        #region 登录
        /// <summary>
        /// 登录操作
        /// </summary>
        /// <param name="uname"></param>
        /// <param name="upwd"></param>
        /// <returns></returns>
        public async Task<IActionResult> LoginDo(Member u)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(u.UserPwd) || string.IsNullOrWhiteSpace(u.UserPwd))
                {
                    return Json(ResultInfo.Error("用户名或密码为空！"));
                }
                var model = Member.GetList().Where(x => x.UserName == u.UserName && x.UserPwd == u.UserPwd).FirstOrDefault();
                if (model == null)
                {
                    return Json(ResultInfo.Error("用户不存在，登录失败！！"));
                }
                //把需要保存的用户数据转成一个字符串。
                var data = JsonConvert.SerializeObject(model);
                //用户标识
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                identity.AddClaim(new Claim(ClaimTypes.Sid, model.Id.ToString()));
                identity.AddClaim(new Claim(ClaimTypes.Name, model.UserName));
                identity.AddClaim(new Claim(ClaimTypes.UserData, data));
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
                return Json(ResultInfo.Success("登录成功！"));
            }
            catch (Exception ex)
            {

                return Json(ResultInfo.Error("用户不存在，登录失败！！"));
            }
        }
        #endregion

        #endregion
    }
}